package com.manner.groupon.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.DeleteRequest;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.model.entity.Categories;
import com.manner.groupon.service.goods.CategoriesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Manner
 * @Description: 商品分类请求控制器
 * @DateTime: 2024/10/6 16:28
 */
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
    
public class CategoriesController {

    private final CategoriesService categoriesService;

    /**
     * 获取一级分类数据
     *
     * @return BaseResponse<List < Categories>>
     * @author Manner
     * @date 2024/10/8 23:14
     */
    @GetMapping("/one")
    public BaseResponse<List<Categories>> getPrimaryClassification() {
        List<Categories> oneCategories = categoriesService.list(
                new LambdaQueryWrapper<Categories>()
                        .isNull(Categories::getParentId));
        return ResultUtils.success(oneCategories);
    }

    /**
     * 获取二级分类
     *
     * @return BaseResponse<List < Categories>>
     * @author Manner
     * @date 2024/10/8 23:15
     */
    @GetMapping("/two")
    public BaseResponse<List<Categories>> getSecondaryClassification() {
        List<Categories> twoCategories = categoriesService.list(
                new LambdaQueryWrapper<Categories>()
                        .isNotNull(Categories::getParentId));
        return ResultUtils.success(twoCategories);
    }

    /**
     * 删除分类
     *
     * @param deleteRequest
     * @return Integer
     * @author Manner
     * @date 2024/10/8 23:27
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteCategories(DeleteRequest deleteRequest) {
        return ResultUtils.success(categoriesService.removeById(deleteRequest.getDataId()));
    }
}
